package merchants.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import merchants.constant.ErrorCode;
import merchants.dao.MerchantsDao;
import merchants.entity.Merchants;

/**
 * 创建商户请求对象
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CreateMerchantsRequest {

    /** 商户名称 */
    private String name;
    /**  商户logo*/
    private String logoUrl;
    /** 商户营业执照 */
    private String businessLicenseUrl;
    /** 商户联系电话 */
    private String phone;
    /** 商户地址 */
    private String address;

    /**
     * 验证请求有限性
     * @param merchantsDao {@link MerchantsDao}
     * @return {@link ErrorCode}
     */
    public ErrorCode validate(MerchantsDao merchantsDao){

        if(merchantsDao.findByName(name) != null || name == null)
            return ErrorCode.DUPLICATE_NAME;
        if(logoUrl == null)
            return ErrorCode.EMPTY_LOGO;
        if(businessLicenseUrl == null)
            return ErrorCode.EMPTY_BUSINESS_LICENSE;
        if(address == null)
            return ErrorCode.EMPTY_ADDRESS;
        if(phone == null)
            return ErrorCode.ERROR_PHONE;

        return ErrorCode.SUCCESS;
    }

    /**
     * 将请求对象转换为商户对象
     * @return {@link Merchants}
     */
    public Merchants toMerchants(){

        Merchants merchants = new Merchants();
        merchants.setName(name);
        merchants.setLogoUrl(logoUrl);
        merchants.setBusinessLicenseUrl(businessLicenseUrl);
        merchants.setPhone(phone);
        merchants.setAddress(address);

        return merchants;
    }
}
